﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Base.Entity;
using T.ServiceComponent.DAC.Service;
using System.Reflection;
using System.Data;

namespace T.FAS.Runtime.Base.Dao
{
    public class MaskSettingDao : DaoService<MaskSetting>
    {
        public override string TableName => "FAS_MaskSetting";
        public override string IDColumnName => "ID";
        public override List<string> ExceptColumnName => new List<string> { };

        private readonly MaskSettingDetailDao _maskSettingDetailDao = new MaskSettingDetailDao();

        public MaskSettingDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 从
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MaskSetting GetRuleById(string id)
        {
            var dt = base.GetByFilter($"{IDColumnName}='{id}'");
            var result = DaoUtil.ConvertToEntity<MaskSetting>(dt, this.ExceptColumnName)?.FirstOrDefault();
            if (result != null && result.IsRegionRelated)
            {
                result.Details = _maskSettingDetailDao.GetRuleDetailsByForeignKey(id);
            }
            return result;
        }

        /// <summary>
        ///  
        /// </summary>
        /// <returns></returns>
        public List<MaskSetting> GetAllRules()
        {
            var dt = base.GetByFilter($"1=1");
            var result = DaoUtil.ConvertToEntity<MaskSetting>(dt, this.ExceptColumnName);
            if (result?.Count > 0)
            {
                var details = _maskSettingDetailDao.GetAllRuleDetails();
                if (details?.Count > 0)
                {
                    result.ForEach(r =>
                    {
                        r.Details = details.Where(t => t.MaskID == r.ID).ToList();
                    });
                }
            }
            return result;
        }
    }
}
